package com.asj.service.control;

import android.annotation.SuppressLint;
import android.widget.ProgressBar;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.asj.MainActivity;
import com.asj.R;
import com.asj.adapter.ProgressAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 进度条组件控制服务
 * */
public class ControlProgressBarService {
    @SuppressLint("StaticFieldLeak")
    private static ProgressBar progressBar;
    private static RecyclerView recyclerView;

    private static List<String> progressSteps;
    private static ProgressAdapter progressAdapter;
    private static MainActivity mainActivity;

    public static void setMainActivity(MainActivity mainActivity) {
        if(ControlProgressBarService.mainActivity == null){
            ControlProgressBarService.mainActivity = mainActivity;
        }
    }

    /**
     * 获取进度条和进度列表组件
     * */
    public static void setProgressBar(MainActivity mainActivity){
        setMainActivity(mainActivity);
        mainActivity.runOnUiThread(() -> {
            // 在这里更新UI
            progressBar = mainActivity.findViewById(R.id.progressBar);
        });
    }

    /**
     * 获取进度列表组件
     * */
    public static void setRecyclerView(MainActivity mainActivity){
        setMainActivity(mainActivity);
        mainActivity.runOnUiThread(() -> {
            // 在这里更新UI
            recyclerView = mainActivity.findViewById(R.id.recyclerView);
            // 初始化列表
            recyclerView.setLayoutManager(new LinearLayoutManager(mainActivity));
            recyclerView.setHasFixedSize(true);

            // 初始化列表和适配器
            progressSteps = new ArrayList<>();

            progressAdapter = new ProgressAdapter(progressSteps);
            recyclerView.setAdapter(progressAdapter);
        });
    }

    /**
     * 初始化进度条长度
     * */
    public static void initProgressBarValue(int maxValue,int initValue,MainActivity mainActivity){
        setMainActivity(mainActivity);
        // 设置进度步初始进度
        mainActivity.runOnUiThread(() -> {
            // 在这里更新UI
            progressBar.setMax(maxValue); // 设置进度条的最大值
            progressBar.setProgress(initValue); // 初始进度为0
        });
    }
    public static void setProgressBarMax(int maxValue,MainActivity mainActivity){
        setMainActivity(mainActivity);
        // 设置进度步初始进度
        mainActivity.runOnUiThread(() -> {
            // 在这里更新UI
            progressBar.setMax(maxValue); // 设置进度条的最大值
        });
    }


    /**
     * 设置进度以及进度列表的数据
     * @param progress 进度数值
     * @param progressMessage 进度描述
     * */
    public static void setProgressBarAndListValue(int progress,
                                                  String progressMessage){
        mainActivity.runOnUiThread(()->{
            progressBar.post(()->{
                // 在这里更新UI
                progressBar.setProgress(progress);
                progressSteps.add(progressMessage);
                progressAdapter.notifyItemInserted(progressSteps.size() - 1);
            });
            recyclerView.post(() -> {
                // 将滚动位置设置到底部
                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                assert layoutManager != null;
                int position = layoutManager.getItemCount() - 1;
                if (position >= 0) {
                    recyclerView.scrollToPosition(position);
                }
            });
        });
    }
}
